package com.project.action.user;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.text.View;

import org.hibernate.Interceptor;
import org.hibernate.Session;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.project.action.LoginAction;
import com.project.service.UserService;
import com.project.util.MD5Util;
import com.project.util.SendMail;
import com.project.util.StringUtil;
import com.project.vo.Admin;
import com.project.vo.Product;
import com.project.vo.User;
import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentFilter;

import net.sf.json.JSONObject;

public class UserAction extends ActionSupport{

	private Integer id;
	private Integer oldId;
	private String name;
	private String oldName;
	private String password;
	private BigInteger phone;
	private String email;
	private String IDCard;
	private String description;
	
	private String code;
	private String json;
	
	private Integer pageNum;
	private UserService userService;
	private Map<String, Object> request;
	private Map<String, Object> session;
	
	public void getContext() {
		// TODO Auto-generated constructor stub
		ActionContext context = ActionContext.getContext();
		request = (Map<String, Object>) context.get(StringUtil.REQUEST);
		session = context.getSession();
	}
	public User getUserBySession(){
		User user = new User();
		user = (User)session.get(StringUtil.USER);
		return user;
	}
	
	/**
	 * forget and find password
	 * @return
	 */
	public String findPassword(){
		getContext();
		//组装json数据
		Map<String, Object> map = new HashMap<>();
		//获取数据库中的验证码
//		String codeFromSql = userService.getCode(email);
//		if (!codeFromSql.equals(code)) {
//			map.put(StringUtil.RESULT, StringUtil.CODEERROR);
//		}else{
//			String result = userService.findPassword(id, password);
//			if (result.equals(StringUtil.SUCCESS)) {
//				User user = userService.login(id, password);
//				if (user == null) {
//					session.put(StringUtil.LOGINSTATE, StringUtil.LOGOUT);
//					return StringUtil.FAILURE;
//				}
//				session.put(StringUtil.USER, user);
//				session.put(StringUtil.LOGINSTATE, StringUtil.LOGIN);
//				map.put(StringUtil.RESULT, StringUtil.SUCCESS);
//			}else {
//				map.put(StringUtil.RESULT, StringUtil.FAILURE);
//			}
//		}
		String result = userService.findPassword(id,password);
		if (result.equals(StringUtil.SUCCESS)) {
			User user = userService.login(id, password);
			if (user == null) {
				session.put(StringUtil.LOGINSTATE, StringUtil.LOGOUT);
				return StringUtil.FAILURE;
			}
			session.put(StringUtil.USER, user);
			session.put(StringUtil.LOGINSTATE, StringUtil.LOGIN);
		}
		map.put(StringUtil.RESULT, result);

		JSONObject jsonObject = JSONObject.fromObject(map);
		json = jsonObject.toString(); 	//json必须有setter和getter
		return StringUtil.SUCCESS;
	}
	public String sendCode(){
		//存储email code
		Map<String, Object> map = new HashMap<>();
		request = (Map<String, Object>) ActionContext.getContext().get("request");
		String code = SendMail.getRandom();
		String content = StringUtil.CONTENT + code;
		String subject = StringUtil.SUBJECT;
		
//		String result = userService.saveCode(email, code);
//		if (result.equals(StringUtil.SUCCESS)) {
//			map.put(StringUtil.RESULT, StringUtil.SUCCESS);
//			SendMail.send(content, email, subject);
//		}else{
//			map.put(StringUtil.RESULT, StringUtil.FAILURE);
//		}
		Boolean isSend = SendMail.send(content, email, subject);
		if(!isSend){
			return StringUtil.FAILURE;
		}
		code = MD5Util.MD5Encode(code,StringUtil.CODING, false);
		map.put(StringUtil.CODE, code);

		JSONObject jsonObject = JSONObject.fromObject(map);
		json = jsonObject.toString(); 	//json必须有setter和getter
		//返回code
		return StringUtil.SUCCESS;
	}
	
	/**
	 * 获取MD5
	 * @return
	 */
	public String getMD5() {//mail
		Map<String, Object> map = new HashMap<>();
		request = (Map<String, Object>) ActionContext.getContext().get("request");
		String codeMd5 = MD5Util.MD5Encode(code,StringUtil.CODING, false);
		map.put(StringUtil.CODE, codeMd5);
		JSONObject jsonObject = JSONObject.fromObject(map);
		json = jsonObject.toString(); 	//json必须有setter和getter
		return StringUtil.SUCCESS;
	}

	/**
	 * return to edit page
	 * @return
	 */
	public String editJSP(){
		return view();
	}
	/**
	 * update user's personal info
	 * @return
	 */
	public String update() {
		User user = userService.view(oldId);
		user.setName(name);
		user.setPhone(phone);
		user.setEmail(email);
		user.setDescription(description);
		user.setIDCard(IDCard);
		String result = userService.update(oldId, user);
		if (result.equals(StringUtil.SUCCESS)) {
			return view();
		}else {
			return StringUtil.FAILURE;
		}
	}
	/**
	 * view user's personal info
	 * @return
	 */
	public String view() {
		getContext();
		String result = StringUtil.FAILURE;
		if (id == null) {
			id = getUserBySession().getId();
		}
		User user = userService.view(id);
		if (user != null) {
			session.put(StringUtil.USER, user);
			request.put(StringUtil.USER, user);
			result = StringUtil.SUCCESS;
		}
		return result;
	}
	public String view_A() {
		getContext();
		String result = StringUtil.FAILURE;
		Admin admin = (Admin)session.get(StringUtil.ADMIN);
		admin = userService.view_A(admin.getId());
		if (admin != null) {
			session.put(StringUtil.ADMIN, admin);
			result = StringUtil.SUCCESS;
		}
		return result;
	}
	public String view_All() {
		getContext();
		List<User> users = new ArrayList<>();
		List<User> allUsers = new ArrayList<>();
		allUsers = userService.getAllUser();
		if (null == pageNum) {
			pageNum = 1;
		}
		users = userService.getAllUser(pageNum, StringUtil.PAGESIZE);
		request.put(StringUtil.USERS, users);
		request.put(StringUtil.COUNTNUM, allUsers.size());
		return StringUtil.SUCCESS;
	}
	
	public String pageChange(){
		getContext();
		Map<String, Object> map = new HashMap<>();
		List<User> users = new ArrayList<User>();
		users = userService.getAllUser(pageNum, StringUtil.PAGESIZE);
		map.put(StringUtil.RESULT, users);
		JSONObject  jsonObject = JSONObject.fromObject(map);
		json = jsonObject.toString();
		return StringUtil.SUCCESS;
	}
	public String delete() {
		String result = StringUtil.FAILURE;
		result = userService.delete(id);
		if (result.equals(StringUtil.FAILURE)) {
			return result;
		}
		return view_All();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public Map<String, Object> getRequest() {
		return request;
	}
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getOldName() {
		return oldName;
	}

	public void setOldName(String oldName) {
		this.oldName = oldName;
	}

	public BigInteger getPhone() {
		return phone;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setPhone(BigInteger phone) {
		this.phone = phone;
	}

	public Integer getPageNum() {
		return pageNum;
	}
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getIDCard() {
		return IDCard;
	}

	public void setIDCard(String iDCard) {
		IDCard = iDCard;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	public Integer getOldId() {
		return oldId;
	}
	public void setOldId(Integer oldId) {
		this.oldId = oldId;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
}
